package com.shenhua.test.config;

import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger配置
 * @author Sequence
 * @date 2021/08/05 16:52
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public ApiInfo getApiInfo() {
        return new ApiInfoBuilder()
                .title("测试")
                .description("测试代码")
                .build();
    }

    @Bean
    public Docket getDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(this.getApiInfo())
                .select()
                // 设置需要被扫描的类，这里设置为添加了@Api注解的类
//                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(getParameters());
    }

    private List<Parameter> getParameters() {
        ParameterBuilder parameterBuilder1 = new ParameterBuilder();
        List<Parameter> list = new ArrayList<>();
        Parameter build1 = parameterBuilder1.name("userId").description("userId").modelRef(new ModelRef("string")).parameterType("header").required(true).build();

        ParameterBuilder parameterBuilder2 = new ParameterBuilder();
        Parameter build2 = parameterBuilder2.name("userName").description("userName").modelRef(new ModelRef("string")).parameterType("header").required(true).build();

        ParameterBuilder parameterBuilder3 = new ParameterBuilder();
        Parameter build3 = parameterBuilder3.name("realName").description("realName").modelRef(new ModelRef("string")).parameterType("header").required(true).build();

        ParameterBuilder parameterBuilder4 = new ParameterBuilder();
        Parameter build4 = parameterBuilder4.name("tenant-id").description("tenant-id").modelRef(new ModelRef("string")).parameterType("header").required(true).build();

        list.add(build1);
        list.add(build2);
        list.add(build3);
        list.add(build4);
        return list;

    }
}
